// pages/homework/wrongQuestion/wrongQuestion.js
import https from '../../../utils/request4.js'
import http6 from '../../../utils/request6.js'
import http from '../../../utils/request.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    examId: null,
    title: null,
    gradeid: null,
    courseid: null,
    classId: null,
    uuidList: [],
    questionInfo: [],
    questionList: [],
    nodes: null,
    richTextNodes: null,
    viewSrc: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
        examId: options.examId,
        title: options.title,
        gradeid: options.gradeid,
        courseid: options.courseid,
        classId: options.classId,
      })
      let viewSrc = `https://heq.bsedu.com/?token=Bearer ${JSON.parse(wx.getStorageSync('token'))}&courseid=${this.data.courseid}&examId=${this.data.examId}&classId=${this.data.classId}`
      console.log(viewSrc,'________________________')
      this.setData({
        viewSrc
      })
    // this.getInfo()
  },
  switchStatus(e) {
    console.log(e.currentTarget.dataset.value,e.currentTarget.dataset.index);
    if (e.currentTarget.dataset.value == 1) {
        // 解析
        let questionList = JSON.parse(JSON.stringify(this.data.questionList))
        questionList[e.currentTarget.dataset.index].analysisFlag = !questionList[e.currentTarget.dataset.index].analysisFlag
        this.setData({
            questionList:questionList
        })
    } else {
        // 得分
        let questionList = JSON.parse(JSON.stringify(this.data.questionList))
        questionList[e.currentTarget.dataset.index].scoreFlag = !questionList[e.currentTarget.dataset.index].scoreFlag
        this.setData({
            questionList:questionList
        })
    }
  },
  renderContent(content) {
    const segments = parseMixedContent(content);
    const nodes = [];

    segments.forEach(segment => {
      switch (segment.type) {
        case 'html':
          nodes.push({
            name: 'view',
            attrs: { class: 'html-segment' },
            children: [{ type: 'text', text: segment.content }]
          });
          break;

        case 'latex':
          nodes.push({
            name: 'div',
            attrs: { class: 'latex-formula' },
            children: parse(segment.content)
          });
          break;

        default:
          nodes.push({
            name: 'view',
            attrs: { class: 'text-segment' },
            children: [{ type: 'text', text: segment.content }]
          });
      }
    });

    this.setData({ richTextNodes: nodes });
  },
  async getInfo() {
    //   获取所有题目的uuid
    const rawText =
  "<view>222222222</view>这是一个行内公式 $ f(x) = sum_{n=0}^{infty} \\frac{f^{(n)}(a)}{n!} (x - a)^n $ ，这是一个行间的公式 $$ \\int_a^b f(x) , dx = F(b) - F(a) $$ \n这是新起一行的文字";
  this.renderContent(rawText);
    let data = {
        url:`/work/questionids?examCourseId=${this.data.courseid}`,
        method:'get',
        header:{"content-type":"application/json;charset=UTF-8"}
    }
    let ress = await https(data)  // Now this await is valid
    if(ress&&ress.data.code==200){
      if(ress.data.data!=null){
        this.setData({
            uuidList: ress.data.data
        })
      }
    } else {
        wx.showToast({
            title: ress.data.message,
            icon: 'none',
            duration: 2000 //持续的时间
        })
    }
    // 根据uuid获取题目信息
    let dataQuestion = {
        url:`/system/question/info/list/ids`,
        method:'post',
        data:{questionUUids: this.data.uuidList},
        header:{"content-type":"application/json;charset=UTF-8"}
    }
    let res = await http6(dataQuestion)  // Now this await is valid
    if(res&&res.data.code==200){
      if(res.data.data!=null){
        let questionInfo = JSON.parse(JSON.stringify(res.data.data))
        res.data.data.map(datares => {
            if (datares.children != null) {
                questionInfo = questionInfo.concat(datares.children)
            }
        })
        // questionInfo.map(ressitem => {
        //     const nodes = renderMathInText(ressitem.contentPageShow, {
        //         delimiters: [
        //           { left: "$$", right: "$$", display: true },
        //           { left: "$", right: "$", display: false },
        //         ],
        //     });
        //     ressitem.contentPageShow = nodes
        // })
        this.setData({
            questionInfo: questionInfo
        })
      }
    } else {
        wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000 //持续的时间
        })
    }

    // 获取试题得分信息
    let dataitem = {
        url:`/score/error-distribution-detail/list/${this.data.examId}?classId=${this.data.classId}&questionId=0`,
        method:'get',
        header:{"content-type":"application/json;charset=UTF-8"}
    }
    let item = await http(dataitem)  // Now this await is valid
    if(item&&item.data.code==200){
      if(item.data.data!=null){
        let questionList = JSON.parse(JSON.stringify(item.data.data))
        questionList.map(response => {
            response.analysisFlag = false
            response.scoreFlag = false
            let question = this.data.questionInfo.filter(question => {
                return question.uuid == response.questionUUid
            })
            if (question.length > 0) {
                response.question = question[0]
            }
        })
        this.setData({
            questionList: questionList
        })
        console.log(this.data.questionList,'------------------------');
      }
    } else {
        wx.showToast({
            title: item.data.message,
            icon: 'none',
            duration: 2000 //持续的时间
        })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})